local dx_map = require("dx_map")
local dx_thread = require("dx_thread")
local dx_queue = require("dx_queue")
local logger = require("dx_logger")
local gpio_service = require("gpio_service")
local network_service = require("network_service")
local sqllite3_service = require("sqllite3_service")
local config = require("config")
local dx_json = require("dx_json")
local time = require("time")
local start_service = require("start_service")
local dx_watchdog = require("dx_watchdog")



local function run()

    logger.info("服务开始初始化")
    -- 初始化队列
    _G.mqtt_queue = dx_queue.create(100)
    _G.channel_queue = dx_queue.create(100)

    -- 初始化全局map
    _G._G_map = dx_map.create()
    logger.info("[全局map]初始化完成")

    -- 初始化配置
    config.init()
    logger.info("[系统配置]初始化完成")

    local mqtt_dog, serialport_dog = "Y", "Y"
    dx_map.set(_G_map, "mqtt_dog", mqtt_dog)
    dx_map.set(_G_map, "serialport_dog", serialport_dog)

    -- 初始化sqllite
    sqllite3_service.init()
    dx_map.set(_G_map, "sqlite_client", sqlite_client)
    logger.info("[sqlite实例]初始化完成")

    -- 初始化网络
    network_service.config_net()

    time.calibration_time()
    logger.info("时间校准完成")

    -- 初始化串口ttyS0
    dx_thread.create("../../dx_cc101_app/src/common/channel_common.lua", {_G_map = _G_map, ___config_map = ___config_map, mqtt_queue = mqtt_queue, channel_queue = channel_queue})
    logger.info("[ttyS0串口监听]初始化完成")

    -- 初始化串口ttyS1
    -- dx_thread.create("../../dx_cc101_app/src/common/channel_common.lua", {_G_map = _G_map, config_map = config_map, mqtt_queue = mqtt_queue, channel_queue = channel_queue})
    logger.info("[ttyS1信道监听]初始化完成")

    -- 初始化mqtt监听
    dx_thread.create("../../dx_cc101_app/src/common/mqtt_common.lua", {_G_map = _G_map, ___config_map = ___config_map, mqtt_queue = mqtt_queue, channel_queue = channel_queue})
    logger.info("[mqtt监听]初始化完成")

    -- 初始化&定义gpio
    gpio_service.config_gpio()
    logger.info("[gpio]初始化完成")


    -- -- 初始化看门狗
    -- local wdt = dx_watchdog.watchdog_open(1, 0)
    -- dx_watchdog.watchdog_enable(wdt, 1, true)
    -- dx_watchdog.watchdog_start(wdt, 10000)
    -- logger.info("[看门狗]初始化完成")


    -- -- 设备启动状态上送，硬狗和断电的情况下,返回true
    -- start_service.running_status(dx_watchdog.watchdog_is_poweron(wdt))
    -- logger.info("[running]设备启动状态处理")

    -- 启动蓝牙扫描
    dx_queue.push(
        channel_queue,
        dx_json.encode({cmd = "C1", data = "01" .. config.get("ble_type") .. "3A" .. config.get("ble_rssi")}),
        500
    )

    while true do
        os.execute("sleep " .. 10)

        -- if
        --     (dx_map.get(_G_map, "mqtt_dog") == "Y" or dx_map.get(_G_map, "mqtt_connect_ing") == "Y") and
        --         dx_map.get(_G_map, "serialport_dog") == "Y"
        --  then
        --     -- 喂狗
        --     dx_watchdog.watchdog_restart(wdt, 1)
        --     dx_map.set(_G_map, "mqtt_dog", "N")
        --     dx_map.set(_G_map, "serialport_dog", "N")
        -- end
        logger.info("[app_main]正在运行")
    end

end

-- 释放资源
local function exit()
    sqllite3_init.exit()
end

-- 开始运行主逻辑
run()

-- 服务结束前释放资源
exit()
